package com.yinp.proapp.module.study.recycler.activity;

import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.yinp.proapp.adapter.CommonAdapter;
import com.yinp.proapp.adapter.SingleViewHolder;
import com.yinp.proapp.base.activity.PresenterWanAndroidActivity;
import com.yinp.proapp.bean.UploadFile;
import com.yinp.proapp.databinding.ActivityMoreRecyclerActivityityBinding;
import com.yinp.proapp.databinding.ItemARecyclerBinding;
import com.yinp.proapp.module.study.recycler.bean.ABean;
import com.yinp.proapp.module.study.recycler.bean.UpLoadImage;
import com.yinp.proapp.utils.MTextWatcher;
import com.yinp.proapp.utils.SelectPhotos;
import com.yinp.proapp.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.photopicker.activity.BGAPhotoPickerActivity;
import cn.bingoogolapple.photopicker.widget.BGASortableNinePhotoLayout;

/**
 * @Author: yinp
 * @Date: 2021/8/19
 * @Description:多个RecyclerView嵌套操作
 */
public class MoreRecyclerActivity extends PresenterWanAndroidActivity<ActivityMoreRecyclerActivityityBinding> {
    private List<ABean> aList = new ArrayList<>();
    private CommonAdapter<ABean> aAdapter;
    private LinearLayoutManager llm;

    @Override
    protected void initViews() {
        setStatusBarHeight(StatusBarUtil.getStatusBarHeight(mContext));
        initClick(bd.header.headerBackImg, bd.btnAdd, bd.btnDelete, bd.btnAdd2);
        initRecycler();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == bd.header.headerBackImg) {
            finish();
        } else if (v == bd.btnAdd) {
            aList.add(new ABean(aList.size()+"  长按删除"));
            aAdapter.notifyItemInserted(aList.size() - 1);
            bd.rvOne.smoothScrollToPosition(aList.size() - 1);
        } else if (v == bd.btnDelete) {
            if (aList.size() == 0) {
                return;
            }
            aList.remove(aList.size() - 1);
            aAdapter.notifyItemRemoved(aList.size());
        } else if (bd.btnAdd2 == v) {
//            滑动碧recyclerView的更平滑
            aList.add(new ABean(aList.size()+"  长按删除"));
            aAdapter.notifyItemChanged(aList.size() - 1);
            llm.scrollToPositionWithOffset(aList.size() - 1, 0);
            //用于反转添加
//            llm.setStackFromEnd(true);
        }
    }

    private void initRecycler() {
        aAdapter = new CommonAdapter<ABean>(mContext, aList) {
            @Override
            protected SingleViewHolder setComViewHolder(View view, int viewType, ViewGroup parent) {
                ItemARecyclerBinding binding = ItemARecyclerBinding.inflate(mInflater, parent, false);
                AViewHolder holder = new AViewHolder(binding, aList, mActivity);
                holder.itemView.setOnLongClickListener(v -> {
                    aList.remove(holder.getAbsoluteAdapterPosition());
                    aAdapter.notifyItemRemoved(holder.getAbsoluteAdapterPosition());
                    return false;
                });
                return holder;
            }

            @Override
            public void onBindItem(SingleViewHolder holder, int position, ABean item) {
                AViewHolder viewHolder = (AViewHolder) holder;
                viewHolder.binding.etText.setText("");
                viewHolder.binding.tvTitle.setText(item.getTitle());
                viewHolder.binding.tvTitle.setTag(viewHolder.getItemId());
                /**
                 * 选取图片
                 */
                viewHolder.binding.itemPhotos.setData(null);
                aList.get(viewHolder.getAbsoluteAdapterPosition()).setItemPhotos(viewHolder.binding.itemPhotos);
                viewHolder.binding.itemPhotos.setMaxItemCount(12);
                viewHolder.binding.itemPhotos.setTag(viewHolder.getItemId());
                viewHolder.binding.etText.addTextChangedListener(new MTextWatcher() {
                    @Override
                    public void afterTextChanged(Editable s) {
                        aList.get(viewHolder.getAbsoluteAdapterPosition()).setContent(s.toString());
                    }
                });
            }
        };
        llm = new LinearLayoutManager(this);
        bd.rvOne.setLayoutManager(llm);
        bd.rvOne.setAdapter(aAdapter);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (data != null) {
                showLoading("上传中...");
                ArrayList<String> list = new ArrayList<>();
                list.clear();
                list.addAll(BGAPhotoPickerActivity.getSelectedPhotos(data));
                ini2(list, requestCode);
//                SelectPhotos.compressImage(mContext, list, imageList -> SelectPhotos.uploadFile(MoreRecyclerActivity.this, presenter, imageList
//                        , requestCode, new SelectPhotos.AA() {
//                            @Override
//                            public void getValue(List<UploadFile> list, int position) {
//                                ini(list, position);
//                            }
//                        }));
            }
        }
    }

    public void ini(List<UploadFile> list, int position) {
        ArrayList<String> listPath = new ArrayList<>();//此次上传的图片地址集合
        List<UpLoadImage> listImage = new ArrayList<>();//此次上传的图片id集合
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {//添加此次上传的数据
                listPath.add(list.get(i).getAccessUrl());
                listImage.add(new UpLoadImage(String.valueOf(list.get(i).getFileId()), list.get(i).getAccessUrl()));
            }
        }
        ABean bean = aList.get(position);
        listPath.addAll(bean.getItemPhotos().getData());
        if (bean.getImageList().size() > 0) {
            listImage.addAll(bean.getImageList());
        }
        bean.getImageList().clear();
        bean.getImageList().addAll(listImage);
        bean.getItemPhotos().setData(listPath);
    }

    public void ini2(ArrayList<String> list, int position) {
        hideLoading();
        ArrayList<String> listPath = list;//此次上传的图片地址集合
        List<UpLoadImage> listImage = new ArrayList<>();//此次上传的图片id集合
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {//添加此次上传的数据
                listImage.add(new UpLoadImage("", list.get(i)));
            }
        }
        ABean bean = aList.get(position);
        listPath.addAll(bean.getItemPhotos().getData());
        if (bean.getImageList().size() > 0) {
            listImage.addAll(bean.getImageList());
        }
        bean.getImageList().clear();
        bean.getImageList().addAll(listImage);
        bean.getItemPhotos().setData(listPath);
    }

    public static class AViewHolder extends SingleViewHolder {
        ItemARecyclerBinding binding;

        public AViewHolder(ItemARecyclerBinding itemView, List<ABean> list, Activity mActivity) {
            super(itemView);
            binding = itemView;
            binding.itemPhotos.setDelegate(new BGASortableNinePhotoLayout.MDelegate() {
                @Override
                public void onClickAddNinePhotoItem(BGASortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, ArrayList<String> models) {
                    SelectPhotos.checkPermission(mActivity, sortableNinePhotoLayout, ((Long) binding.itemPhotos.getTag()).intValue());
                }

                @Override
                public void onClickDeleteNinePhotoItem(BGASortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, String model, ArrayList<String> models) {
                    sortableNinePhotoLayout.removeItem(position);
                    list.get(((Long) binding.itemPhotos.getTag()).intValue()).getImageList().remove(position);
                }

                @Override
                public void onClickNinePhotoItem(BGASortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, String model, ArrayList<String> models) {

                }
            });
        }
    }
}